//
//  OwnerViewController.m
//  BasketballClub
//
//  Created by 孙鸿吉 on 16/1/12.
//  Copyright © 2016年 孙鸿吉. All rights reserved.
//

#import "OwnerViewController.h"
#import "OwnerView.h"
#import "LoginViewController.h"
#import "StyleViewController.h"
#import "CollectViewController.h"
#import "ReturnViewController.h"
#import "AboutMeViewController.h"
#import <AVOSCloud.h>
<<<<<<< HEAD
#import <SDImageCache.h>
@interface OwnerViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate,UIGestureRecognizerDelegate>
=======
@interface OwnerViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate,UIGestureRecognizerDelegate,UIActionSheetDelegate>
>>>>>>> 9f0c383b5ea6663d8e215c06d56da7c42a448b3a
{
    //用来判断点击设置图片的按钮弹出来2个button 用来区分点击的是哪个button好执行不同的图片赋值
    BOOL _isTouchButton1;
    BOOL _isTouchButton2;
}
@property (nonatomic,strong)OwnerView *ownerView;

@end

@implementation OwnerViewController
- (void)loadView
{
    self.ownerView = [[OwnerView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.ownerView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.ownerView.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:242/255.0 alpha:1.0];
    //设置按钮点击事件
    [self.ownerView.setImageButton addTarget:self action:@selector(pushSetImage) forControlEvents:UIControlEventTouchUpInside];
    //背景图片按钮
    [self.ownerView.changeBackImage addTarget:self action:@selector(changeBackImageAction) forControlEvents:UIControlEventTouchUpInside];
    //个性头像按钮
    [self.ownerView.setStyleImage addTarget:self action:@selector(setStyleImageAction) forControlEvents:UIControlEventTouchUpInside];
    //登陆按钮
    [self.ownerView.loginButton addTarget:self action:@selector(loginButtonAction) forControlEvents:UIControlEventTouchUpInside];
    //个人设置点击方法
    UITapGestureRecognizer *userGR = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(userGRAction)];
    self.ownerView.userView.userInteractionEnabled = YES;
    [self.ownerView.userView addGestureRecognizer:userGR];
    userGR.delegate = self;
    //我的收藏点击方法
    UITapGestureRecognizer *collectGR = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(collectGRAction)];
    self.ownerView.collectView.userInteractionEnabled = YES;
    [self.ownerView.collectView addGestureRecognizer:collectGR];
    collectGR.delegate = self;
    //清除缓存
    UITapGestureRecognizer *clearGR = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clearGRAction)];
    self.ownerView.clearView.userInteractionEnabled = YES;
    [self.ownerView.clearView addGestureRecognizer:clearGR];
    clearGR.delegate = self;
    //反馈
    UITapGestureRecognizer *returnGR = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(returnGRAction)];
    self.ownerView.systemView.userInteractionEnabled = YES;
    [self.ownerView.systemView addGestureRecognizer:returnGR];
    returnGR.delegate = self;
    //关于我们
    UITapGestureRecognizer *aboutMeGR = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(aboutMeGRAction)];
    self.ownerView.aboutMeView.userInteractionEnabled = YES;
    [self.ownerView.aboutMeView addGestureRecognizer:aboutMeGR];
    aboutMeGR.delegate = self;
}
#pragma mark - 界面出现方法
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    //创建用户
    AVUser *current = [AVUser currentUser];
    //如果用户存在 从当前用户关联的服务器文件里面取出图片加载到对应的imageview上
    if (current != nil) {
        AVFile *imagefile = [current objectForKey:@"BackImage"];
        NSData *imageData = [imagefile getData];
        UIImage *headImage = [UIImage imageWithData:imageData];
        if (imageData) {
            self.ownerView.backGroundImageView.image = headImage;
        }else{
            self.ownerView.backGroundImageView.image = [UIImage imageNamed:@"haiyoushui"];
        }

        AVFile *styleFile = [current objectForKey:@"StyleImage"];
        NSData *styleData = [styleFile getData];
        UIImage *styleImage = [UIImage imageWithData:styleData];
        if (styleData) {
            self.ownerView.headImageView.image = styleImage;
        }else{
            self.ownerView.headImageView.image = [UIImage imageNamed:@"head111"];
        }
        self.ownerView.userNickName.text = [current objectForKey:@"styleNickName"];
        self.ownerView.userStyle.text = [current objectForKey:@"styleName"];
        [self.ownerView.loginButton setImage:[UIImage imageNamed:@"iconfont-tuichudenglu"] forState:UIControlStateNormal];
        if ([self.ownerView.userNickName.text isEqualToString:@""]) {
            self.ownerView.userNickName.text = @"篮球";
        }
    }else{
        //如果用户不存在就赋值默认的背景图片
        self.ownerView.backGroundImageView.image = [UIImage imageNamed:@"haiyoushui"];
        self.ownerView.headImageView.image = [UIImage imageNamed:@"head111"];
    }
}

#pragma mark - 登陆按钮点击事件
- (void)loginButtonAction
{
    //创建用户  如果用户不存在就弹出登陆界面
    AVUser *currentUser = [AVUser currentUser];
    if (currentUser == nil) {
        LoginViewController *loginVC = [[LoginViewController alloc]init];
        [self presentViewController:loginVC animated:YES completion:nil];
    }else{
        //登陆的用户点击按钮会注销当前用户同时弹出提示框
        [AVUser logOut];
        [self logoutSuccess];
    }
    
}
#pragma mark - 设置按钮点击事件
- (void)pushSetImage
{
    //点击设置的图片 会弹出2个button
    [UIView animateWithDuration:0.5f animations:^{
        self.ownerView.pushImageView.frame = CGRectMake(0, 0, 90 * LAYOUT_SIZE, 50 * LAYOUT_SIZE_HEIGHT);
    }];
}
#pragma mark - 背景图片按钮
- (void)changeBackImageAction
{
    _isTouchButton1 = YES;
    [self chooseImage];
}
#pragma mark - 个性头像方法
- (void)setStyleImageAction
{
    _isTouchButton1 = NO;
    [self chooseImage];
}
#pragma mark - 实现协议方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    //将选取的图片存到当前用户的本地去 然后同步到服务器
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    AVUser *current = [AVUser currentUser];
    if (_isTouchButton1 == YES ) {
        NSData *imageData = UIImagePNGRepresentation(image);
        AVFile *BackFile = [AVFile fileWithName:@"BackGroundImage.png" data:imageData];
        [current setObject:BackFile forKey:@"BackImage"];
        [current signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            
        }];
    }else{
        NSData *imageData = UIImagePNGRepresentation(image);
        AVFile *StyleFile = [AVFile fileWithName:@"StyleImage.png" data:imageData];
        [current setObject:StyleFile forKey:@"StyleImage"];
        [current signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            
        }];
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

//选取照片的时候 如果取消 会返回我的界面
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
//点击空白处  设置图片的界面会消失
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [UIView animateWithDuration:0.5f animations:^{
        self.ownerView.pushImageView.frame = CGRectMake(0, 0, 0, 0);
    }];
}
#pragma mark - 选取图片方法
- (void)chooseImage
{
    //根据系统版本不同调用不同的提示框方法
    CGFloat verison = [[[UIDevice currentDevice]systemVersion]floatValue];
    if (verison >= 8.0) {
        [self pushAlertController];
    }else{
        [self pushAlertSheet];
    }
}
- (void)pushAlertController
{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"选择方式" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //直接调用本地图片
        UIImagePickerController *picker = [[UIImagePickerController alloc]init];
        //图片选取  是一个枚举 里面3个值 图库 相机 照片库
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        //设置代理
        picker.delegate = self;
        picker.allowsEditing = YES;
        [self presentViewController:picker animated:YES completion:nil];
        
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            UIImagePickerController *picker = [[UIImagePickerController alloc]init];
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            picker.allowsEditing = YES;
            picker.delegate = self;
            [self presentViewController:picker animated:YES completion:nil];
        }else{
        }
        
    }];
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [self dismissViewControllerAnimated:YES completion:nil];
        [UIView animateWithDuration:0.5f animations:^{
            self.ownerView.pushImageView.frame = CGRectMake(0, 0, 0, 0);
        }];
    }];
    
    [alertVC addAction:action1];
    [alertVC addAction:action2];
    [alertVC addAction:action3];
    [self presentViewController:alertVC animated:YES completion:nil];
}
- (void)pushAlertSheet
{
    UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:@"提示" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相机",@"相册", nil];
    [sheet showInView:self.ownerView];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            UIImagePickerController *picker = [[UIImagePickerController alloc]init];
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            picker.allowsEditing = YES;
            picker.delegate = self;
            [self presentViewController:picker animated:YES completion:nil];
        }else{
        }
    }
    if (buttonIndex == 1) {
        //直接调用本地图片
        UIImagePickerController *picker = [[UIImagePickerController alloc]init];
        //图片选取  是一个枚举 里面3个值 图库 相机 照片库
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        //设置代理
        picker.delegate = self;
        picker.allowsEditing = YES;
        [self presentViewController:picker animated:YES completion:nil];

        
    }
    if (buttonIndex == 2) {
        [self dismissViewControllerAnimated:YES completion:nil];
        [UIView animateWithDuration:0.5f animations:^{
            self.ownerView.pushImageView.frame = CGRectMake(0, 0, 0, 0);
        }];
    }
}
#pragma mark - 个人设置点击方法
- (void)userGRAction
{
    AVUser *current = [AVUser currentUser];
    if (current == nil) {
        LoginViewController *loginVC = [[LoginViewController alloc]init];
        loginVC.modalTransitionStyle = 0;
        [self presentViewController:loginVC animated:YES completion:nil];
    }else{
        StyleViewController *styleVC = [[StyleViewController alloc]init];
        [self presentViewController:styleVC animated:YES completion:nil];
    }
}
#pragma mark - 我的收藏点击方法
- (void)collectGRAction
{
    CollectViewController *collectVC = [[CollectViewController alloc]init];
    [self presentViewController:collectVC animated:YES completion:nil];
}
#pragma mark - 清理缓存点击方法
- (void)clearGRAction
{
    
}
#pragma mark - 反馈点击方法
- (void)returnGRAction
{
    ReturnViewController *returnVC = [[ReturnViewController alloc]init];
    [self presentViewController:returnVC animated:YES completion:nil];
}
#pragma mark - 关于我们点击方法
- (void)aboutMeGRAction
{
    AboutMeViewController *aboutMeVC = [[AboutMeViewController alloc]init];
    [self presentViewController:aboutMeVC animated:YES completion:nil];
}


#pragma mark - 提示框(退出登录成功)
- (void)logoutSuccess
{
    CGFloat verison = [[[UIDevice currentDevice]systemVersion]floatValue];
    if (verison >= 8.0) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"退出登录成功" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:nil];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dismissViewControllerAnimated:YES completion:nil];
        });
        
    }else{
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"退出登录成功" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
        [alertView show];
        
        [self performSelector:@selector(aftertimeAction:) withObject:alertView afterDelay:2.0];
    }
    
}
#pragma mark - 延迟2秒方法
- (void)aftertimeAction:(UIAlertView *)sender
{
    [sender dismissWithClickedButtonIndex:0 animated:YES];
    
}























- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - 清除第三方SDWebImage缓存




@end
